<template>
  <div>
    <el-form ref="form" v-loading="loading" :model="form" label-width="100px">
      <el-collapse v-model="activeNames" @change="handleChange">
        <el-collapse-item title="下发分配设置" name="1">
          <el-form-item>
            <el-checkbox v-model="form.is_auto_issue.paramValue">自动分配</el-checkbox>
            <span style="color:#ccc">( 设定是否自动分配线索至网销顾问 )</span>
          </el-form-item>
        </el-collapse-item>
        <!-- <el-collapse-item title="经销商上班时间设置" name="2">
          <el-form-item label="工作时段"
            :rules="[{ required: true, message: '工作时段不能为空', trigger: 'blur'},]">
            <el-time-picker
              is-range
              v-model="form.work_period.paramValue"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              placeholder="选择时间范围">
            </el-time-picker>
          </el-form-item>
          <span style="color:#ccc;padding-left:100px">下发的首次跟进时间和最晚跟进时间,只在上班时间进行计算.</span>
        </el-collapse-item> -->
        <el-collapse-item title="审批设置" name="3">
          <el-form-item>
            <el-checkbox v-model="form.invalid_audit_param.paramValue">无效需要审批</el-checkbox>
            <span style="color:#ccc">( 如需无效审批,需要审批通过后,才会变成无效 )</span>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="form.lose_audit_param.paramValue">战败需要审批</el-checkbox>
            <span style="color:#ccc">( 如需战败审批,需要审批通过后,才会变成战败 )</span>
          </el-form-item>
        </el-collapse-item>

        <!-- <el-collapse-item title="战败原因设置" name="4"> -->
          <!-- <el-row> -->
            <!-- <el-form-item> -->
              <!-- <el-col :span="12">
                <el-checkbox v-model="form.lose_model_param.status"  @change="changeStatus(0)">战败时需选择已购买其他品牌车型</el-checkbox>
                <el-select v-model="form.lose_model_param.paramValue" @change="changeOptions(0,form.lose_model_param.paramValue)" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    :disabled="(item.value == form.lose_city_param.paramValue || item.value == form.lose_dealer_param.paramValue) ? true:false"
                    >
                  </el-option>
                </el-select>
              </el-col> -->
                    <!-- 注释bug号11438  el-checkbox  :disabled="!form.lose_model_param.status"  || el-option  :disabled="(item.value == form.lose_city_param.paramValue || item.value == form.lose_dealer_param.paramValue) ? true:false" -->
              <!-- <el-col :span="12">
                <el-form-item>
                  <el-checkbox v-model="form.lose_city_param.status"  @change="changeStatus(1)">战败时外区域需要选择省份城市</el-checkbox>
                  <el-select v-model="form.lose_city_param.paramValue" @change="changeOptions(1,form.lose_city_param.paramValue)" placeholder="请选择">
                    <el-option
                      v-for="item in options1"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                      :disabled="(item.value == form.lose_model_param.paramValue || item.value == form.lose_dealer_param.paramValue) ? true:false"
                      >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col> -->
                      <!-- 注释bug号11438 el-checkbox :disabled="!form.lose_city_param.status" || el-option :disabled="(item.value == form.lose_model_param.paramValue || item.value == form.lose_dealer_param.paramValue) ? true:false" -->
            <!-- </el-form-item> -->
          <!-- </el-row> -->
          <!-- <el-form-item>
            <el-checkbox v-model="form.lose_dealer_param.status"  @change="changeStatus(2)">战败时需选择本品牌经销商</el-checkbox>
              <el-select v-model="form.lose_dealer_param.paramValue" @change="changeOptions(2,form.lose_dealer_param.paramValue)" placeholder="请选择">
                <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                  :disabled="(item.value == form.lose_model_param.paramValue || item.value == form.lose_city_param.paramValue) ? true:false"
                  >
                </el-option>
              </el-select>
          </el-form-item> -->
                  <!--注释bug号11438  el-checkbox  :disabled="!form.lose_dealer_param.status" ||  el-option :disabled="(item.value == form.lose_model_param.paramValue || item.value == form.lose_city_param.paramValue) ? true:false" -->
        <!-- </el-collapse-item> -->
      </el-collapse>
    </el-form>
    <!-- v-if="savebtn" -->
    <div slot="footer"  style="padding: 20px 0" align="center">
      <el-button @click="save" type="primary">保 存</el-button>
    </div>
  </div>
</template>
<script>
import { searchDefeatReason } from '@/api/potenCus/basedata/defeatreason';
// import { setParameter } from '@/api/potenCus/basedata/cluesPrameter';
import {
  searchOnceDriveMileage,
  saveMileage
} from '@/api/potenCus/basedata/onceDriveMileage';
import moment from 'moment'
export default {
  name: 'setParameters',
  data() {
    return {
      activeNames: ['1', '2','3'],
      options: [],
      value: '',
      options1: [],
      value1: '',
      options2: [],
      value2: '',
      form: {
        // work_period: [new Date(2019, 9, 10, 8 ), new Date(2019, 9, 10, 16)],
        is_auto_issue:{paramValue:''},  //是否线索自动分配
        work_period:{paramValue:[new Date(2019, 9, 10, 8 ,10 ), new Date(2019, 9, 10, 16,10)]},
        invalid_audit_param:{paramValue:''},  //无效需要审批
        lose_audit_param:{paramValue:''},     //战败需要审批
        lose_model_param:{paramValue:'',status:false},   //战败时需选择已购买其他品牌车型
        lose_city_param:{paramValue:'',status:false},    //战败时外区域需要选择省份城市
        lose_dealer_param:{paramValue:'',status:false},  //战败时需选择本品牌经销商
      },
      page: {
        pageNum: 1,
        currentPage: 1,
        pageSize: 1000,
        limit: 1000
      },
      savebtn:false,
      loading:false,
    };
  },
  created () {
    this.getOptions()
    this.searchInfo()
  },
  methods: {
    handleChange(val) {
      console.log(val);
    },
    changeOptions(index,val){
      // console.log(index,val);
      let boo = false;
      if (val) {
        boo = true
      }
      switch (index) {
        case 0:
          this.form.lose_model_param.status = boo
          break;
        case 1:
          this.form.lose_city_param.status = boo
          break;
        case 2:
          this.form.lose_dealer_param.status = boo
          break;
        default:
          break;
      }
      this.savebtn = true;
    },
    changeStatus(index){
      // console.log(index);
      switch (index) {
        case 0:
          this.form.lose_model_param.paramValue = ''
          break;
        case 1:
          this.form.lose_city_param.paramValue = ''
          break;
        case 2:
          this.form.lose_dealer_param.paramValue = ''
          break;
        default:
          break;
      }
      this.savebtn = true;
    },
    async getOptions() {
      this.options=[]
      this.options1=[]
      this.options2=[]
      const res = await searchDefeatReason(this.page)
      // console.log('战败原因' ,res.data.records)
      let arr = []
      res.data.records.forEach(x=>{
        arr.push({
          value: x.defeatReasonId,
          label: x.drDesc
        })
      })
      this.options = this.options1 = this.options2 = arr;
    },
    async searchInfo() {
      this.loading = true;
      let res = await searchOnceDriveMileage();
      console.log('参数设置',res.data)
      var list = res.data
      list.map( (item,index) => {
        if (!this.form[item.paramCode]) {
          this.form[item.paramCode] = {}
        }
        if (item.paramValue === "10041001" || item.paramValue === "10041002") {
          this.form[item.paramCode].paramValue = item.paramValue === "10041001" ? true:false;
        } else if (item.paramCode === "lose_model_param" || item.paramCode === "lose_city_param" || item.paramCode === "lose_dealer_param") {
          this.form[item.paramCode].status = item.status === 10041001 ? true:false;
          this.form[item.paramCode].paramValue = item.status === 10041001 ? Number(item.paramValue):'';
          console.log(this.form[item.paramCode].status)
        } else if (item.paramCode === "work_period") {
          console.log(item.paramValue,"list")
          let date0_0=Number(item.paramValue.split("-")[0].split(":")[0])
          let date0_1=Number(item.paramValue.split("-")[0].split(":")[1])
          let date1_0=Number(item.paramValue.split("-")[1].split(":")[0])
          let date1_1=Number(item.paramValue.split("-")[1].split(":")[1])
          this.form["work_period"].paramValue=[new Date("", "", "", date0_0 ,date0_1 ), new Date("", "", "", date1_0,date1_1)]
        } else {
          this.form[item.paramCode].paramValue = item.paramValue;
        }
        this.form[item.paramCode].paramId = item.paramId;
      })

      this.loading = false;
      console.log(this.form)
    },
    async save() {
      let self = this;
      this.$refs['form'].validate(valid =>{
        if(valid){
          // this.loading = true;
         console.log(this.form,"valid")
          let arr = [],form = this.form;
          Object.keys(form).forEach(function(key){
              // console.log(key,form[key]);
              // console.log(typeof form[key].paramValue)
              if (typeof form[key].paramValue === 'boolean') {
                form[key].paramValue = form[key].paramValue ? '10041001':'10041002'
              }
              if (typeof form[key].status === 'boolean') {
                form[key].status = form[key].status ? 10041001:10041002
              }
              if ( key === 'work_period') {
                if(form['work_period'].paramValue == null || form['work_period'].paramValue.length != 2){
                    form[key].paramValue=form['work_period'].paramValue
                }else{
                    let start= moment(form['work_period'].paramValue[0]).format('HH:mm')
                    let end = moment(form['work_period'].paramValue[1]).format('HH:mm')
                    form[key].paramValue = `${start}-${end}`
                }

              }
              arr.push(form[key])
          });
              // console.log(this.form,"this.form",this.form.is_auto_issue.paramValue)
            // if(arr[0].paramValue == null && (!this.form.invalid_audit_param || !this.form.lose_audit_param) &&(!this.form.lose_model_param || !this.form.lose_city_param || !this.form.lose_dealer_param)  ){

            //   console.log("填写必填项")
            // }
          saveMileage(arr).then(res=>{
            if (res.resultCode === 200) {
              this.loading = false;
              this.savebtn = false;
              this.$message.success('保存成功')
              this.searchInfo()
              // this.closeDialog('refresh');
            } else if(res.resultCode ===500) {
              this.loading = false;
              return false
              this.$message.info("res.errMsg");
            }
          })
        }else{
          this.$message.info("请填写必填项!");
          return false;
        }
      })
    },
  }
}
</script>
<style lang="" scoped>

</style>
